package com.yuncheng.spcyApi.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.dto.SysMenuDto;
import com.yuncheng.spcyApi.entity.SysOrgMenu;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SysMenuMapper;
import com.yuncheng.spcyApi.service.ISysOrgMenuService;
import com.yuncheng.spcyApi.service.IYcSysOrgService;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.OrgMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "部门权限菜单-接口")
@RestController
@RequestMapping(value = "/api/spzx-spcy/orgMenu")
public class SysOrgMenuController {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private ISysOrgMenuService sysOrgMenuService;

    @Resource
    private IYcSysOrgService sysOrgService;

    @Log(title = "查询所有一级菜单")
    @ApiOperation(value = "查询所有一级菜单")
    @GetMapping(value = "queryFistMenuList")
    public HttpResult queryFistMenuList(){
        List<SysMenuDto> topMenuList = sysMenuMapper.findTopMenuList(null);
        return HttpResult.ok(topMenuList);
    }

    @Log(title = "查询所有子级部门")
    @ApiOperation(value = "查询所有子级部门")
    @GetMapping(value = "queryChildrenOrgList")
    public HttpResult queryChildrenOrgList(){

        List<YcSysOrg> list = sysOrgService.list(new LambdaQueryWrapper<YcSysOrg>()
                .isNotNull(YcSysOrg::getParentId)
                .orderByAsc(YcSysOrg::getOrgOrder));

        return HttpResult.ok(list);
    }


    @Log(title = "保存部门权限菜单")
    @ApiOperation(value = "保存部门权限菜单")
    @PostMapping(value = "saveOrgMenu")
    public HttpResult saveOrgMenu(@RequestBody OrgMenuVo orgMenuVo){

        if (StringUtils.isBlank(orgMenuVo.getDeptid())){
            return HttpResult.error("部门参数为空！");
        }

        if (StringUtils.isBlank(orgMenuVo.getMenuid())){
            return HttpResult.error("菜单参数为空！");
        }

        List<SysOrgMenu> omList = sysOrgMenuService.list(new LambdaQueryWrapper<SysOrgMenu>()
                .eq(SysOrgMenu::getDeptid, orgMenuVo.getDeptid())
                .eq(SysOrgMenu::getMenuid, orgMenuVo.getMenuid()));
        if (CollectionUtil.isNotEmpty(omList)){
            sysOrgMenuService.removeById(omList.get(0).getId());
        }else {

            SysOrgMenu newOrgMenu = new SysOrgMenu();
            newOrgMenu.setId(GetUuIdUtils.ReplaceUuId());
            newOrgMenu.setDeptid(orgMenuVo.getDeptid());
            newOrgMenu.setMenuid(orgMenuVo.getMenuid());
            sysOrgMenuService.save(newOrgMenu);
        }

        return HttpResult.ok("保存成功");
    }


    @Log(title = "查询对应部门下的权限菜单")
    @ApiOperation(value = "查询对应部门下的权限菜单")
    @GetMapping(value = "queryOrgIndMenuList")
    public HttpResult queryOrgIndMenuList(@RequestParam(value = "deptid",defaultValue = "")String deptid){
        List<SysOrgMenu> omList = sysOrgMenuService.list(new LambdaQueryWrapper<SysOrgMenu>()
                .eq(SysOrgMenu::getDeptid, deptid));
        if (CollectionUtil.isNotEmpty(omList)){
            List<String> omIds = omList.stream().map(SysOrgMenu::getMenuid).distinct().collect(Collectors.toList());

            return HttpResult.ok(omIds);
        }

        return HttpResult.ok(new ArrayList<String>(1));
    }

}
